namespace PHSRAG.Insight21.Humans
{
	using PHSRAG.Insight21.Context;
	using PHSRAG.Security.Enumerations;
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;
   using Header = PHSRAG.Insight21.InsightWebControls.Header;

	/// <summary>
	///	Launches the functionality to create a new protocol
	/// </summary>
	public partial class CreateNewProtocolBar : System.Web.UI.UserControl
	{
		#region Instance variables
		private Policy.ICachePolicy applicationCachePolicy;
		private Policy.ICachePolicy	pageCachePolicy;
		private Policy.ICachePolicy cachePolicy;

		private const string RefCodeMeaningColumn = "Meaning";
		private const string RefCodeCodeColumn = "Code";

        public string ModuleName = string.Empty;
		#endregion        

        protected void OnPageLoad(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                int moduleRoleBitmask = ((UserContext)cachePolicy.GetCachedObject(UserContext.CacheKey)).ModuleRoleBitmask;
                int moduleRoleBit = (ModuleName.ToLower() == "animals") ? (int)ModuleRoleBit.CreateNewAnimalsProtocolProcess : (int)ModuleRoleBit.CreateNewProtocolProcess;
                               
                if (ModuleName.ToLower() == "animals")
                {
                    // Just to provide a value to NewProtocolApplication in the 
                    // outgoing hash table
                    protocolAppType.Items.Add(Humans.Constants.ModuleType.Animals);
                    protocolAppType.Visible = false;
                    instructions.Text = "To create a new protocol application click the Create New Protocol button.";
                }
                else
                {
                    bool allowEmergencyProtocols = false;
                    if (allowEmergencyProtocols)
                    {
                        // get the protocol types from teh domain, and fill in the drop down with them.
                        CDS.RefCodes refCodes = (CDS.RefCodes)applicationCachePolicy.GetCachedObject(CDS.RefCodes.CacheKey);

                        ArrayList protTypes = refCodes.GetSortedRefCodes(Humans.Constants.NewProtocolType.Domain,
                            Humans.Constants.NewProtocolType.OrderAttribute);

                        protocolAppType.DataSource = protTypes;
                        protocolAppType.DataTextField = RefCodeMeaningColumn;
                        protocolAppType.DataValueField = RefCodeCodeColumn;
                        protocolAppType.DataBind();
                        instructions.Text = "To create a new protocol application, please select a type from the dropdown and click the Create New Protocol button.";
                    }
                    else
                    {
                        protocolAppType.Items.Add(Humans.Constants.NewProtocolType.NewApplication);
                        protocolAppType.Visible = false;
                        instructions.Text = "To create a new protocol application click the Create New Protocol button.";
                    }

                }
               
            }
        }

		#region Web Form Designer generated code
		/// <summary>
		/// OnInit method called when page is initialized.
		/// </summary>
		/// <param name="e">Event arguments.</param>
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);

			applicationCachePolicy = (Policy.ICachePolicy)Application[Policy.Names.ApplicationCachePolicyKey];
			cachePolicy = (Policy.ICachePolicy)Session[Policy.Names.CachePolicyKey];
			pageCachePolicy = (Policy.ICachePolicy)Session[Policy.Names.PageCachePolicyKey];
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.newApp.Click += new System.Web.UI.ImageClickEventHandler(OnClick);
			this.Load += new System.EventHandler(this.OnPageLoad);

		}
		#endregion

		private void OnClick(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			// cache the selected value so the New App page knows which questionnaire to load.
			Hashtable outgoingData = new Hashtable();
			outgoingData.Add(Keys.NewProtocolAppTypeKey, protocolAppType.SelectedValue);
            pageCachePolicy.Cache(Keys.IncomingDataKey, outgoingData);

			Page.Response.Redirect(
				Utility.Settings.GetKeyValue("SubModuleNavigation.Humans.NewProtocolAppURL",""));
		}
	}
}
